package com.sunnada.edu.system.pub.http;

import java.io.File;
import java.io.IOException;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSONObject;

public class SysHttpPost {
	public void add() throws ClientProtocolException, IOException {
		System.out.println("上传人脸头像 ...");

		// 自定义HttpClient 设置CookieStore
		CloseableHttpClient httpclient = HttpClients.custom().build();
		HttpPost request = new HttpPost("http://111.198.71.172:10002/checkin/wisecraftsman/app/ajax/addCollectFace.action");
		File file=new File("D:\\aa.jpg");
		if (file.exists()) {//判断文件目录的存在
	         System.out.println("文件夹存在！");
		}
		// 设置显示头像 并关联到用户
		MultipartEntity reqEntity = new MultipartEntity();
		reqEntity.addPart("userId", new StringBody("13123121321"));
		reqEntity.addPart("userName", new StringBody("test用户"));
		reqEntity.addPart("index", new StringBody("1"));
		//reqEntity.addPart("agenciesid", new StringBody(""));
		//reqEntity.addPart("avatar", new FileBody(new File("D:\\aa.jpg")));
		reqEntity.addPart("face", new FileBody(new File("D:\\aa.jpg")));
		request.setEntity(reqEntity);
		CloseableHttpResponse response = httpclient.execute(request);
		String responseBody = EntityUtils.toString(response.getEntity(), "UTF-8");

		// 解析JSON数据
		JSONObject resp = JSONObject.parseObject(responseBody);
		int result = resp.getIntValue("status");
		if (result == 1) {
			System.out.println("status:" + result + ",message:" + resp.getString("message"));
			System.out.println("facepath:" + resp.getJSONObject("data").getString("facepath"));
		} else {
			System.out.println("status:" + result + ",message:" + resp.getString("message"));
		}
	};

	public void udpate() throws ClientProtocolException, IOException {
		System.out.println("修改显示头像 ...");

		// 自定义HttpClient 设置CookieStore
		CloseableHttpClient httpclient = HttpClients.custom().build();
		HttpPost request = new HttpPost("http://111.198.71.172:10002/checkin/wisecraftsman/app/ajax/updateCollectFace.action");

		// 设置显示头像 并关联到用户
		MultipartEntity reqEntity = new MultipartEntity();
		reqEntity.addPart("userId", new StringBody("1"));
		reqEntity.addPart("userName", new StringBody("test用户"));
		reqEntity.addPart("index", new StringBody("1"));
		reqEntity.addPart("avatar", new FileBody(new File("C:\\Users\\caijin\\Desktop\\人脸比对\\IMG_20161025_113801.jpg")));
		request.setEntity(reqEntity);

		CloseableHttpResponse response = httpclient.execute(request);
		String responseBody = EntityUtils.toString(response.getEntity(), "UTF-8");

		// 解析JSON数据
		JSONObject resp = JSONObject.parseObject(responseBody);
		int result = resp.getIntValue("status");
		if (result == 1) {
			System.out.println("status:" + result + ",message:" + resp.getString("message"));
			System.out.println("facepath:" + resp.getJSONObject("data").getString("facepath"));
		} else {
			System.out.println("status:" + result + ",message:" + resp.getString("message"));
		}
	};

	public void del() throws ClientProtocolException, IOException {
		System.out.println("删除 ...");

		// 自定义HttpClient 设置CookieStore
		CloseableHttpClient httpclient = HttpClients.custom().build();
		HttpPost request = new HttpPost("http://111.198.71.172:10002/checkin/wisecraftsman/app/ajax/delFace.action");

		// 设置显示头像 并关联到用户
		MultipartEntity reqEntity = new MultipartEntity();
		reqEntity.addPart("userId", new StringBody("2321"));
		request.setEntity(reqEntity);

		CloseableHttpResponse response = httpclient.execute(request);
		String responseBody = EntityUtils.toString(response.getEntity(), "UTF-8");

		// 解析JSON数据
		JSONObject resp = JSONObject.parseObject(responseBody);
		int result = resp.getIntValue("status");
		if (result == 1) {
			System.out.println("status:" + result + ",message:" + resp.getString("message"));
		} else {
			System.out.println("status:" + result + ",message:" + resp.getString("message"));
		}
	};

	public static void main(String[] args) throws ClientProtocolException, IOException {
		SysHttpPost test = new SysHttpPost();
		test.add();
		//test.udpate();
		//test.del();
	}
}
